<?php
/**
 * Created by PhpStorm.
 * User: l-eru
 * Date: 2018/6/13
 * Time: 13:30
 */

namespace App;

class Common
{
    const SUCCESS = 0;
    const ERROR = 1;

    /**
     * 请求成功，将数据返回给前端
     *
     * @param array $data
     * @return array
     */
    public static function success($data = [])
    {
        return [
            'code' => self::SUCCESS,
            'data' => $data
        ];
    }


    /**
     * 返回错误信息给前端
     *
     * @param string $message 错误消息
     * @param int $code
     * @return array
     */
    public static function error($message, $code = self::ERROR)
    {
        return [
            'code' => $code,
            'message' => $message
        ];
    }


    /**
     * 生成要发送给configServer的指令
     *
     * @param $fd  int socket文件描述符
     * @param $cmd string 要发送的指令
     * @param $deviceMac string 设备标识
     * @param array $data 要传输的数据， 设备标识会自动传输
     * @param string $from 从哪个服务器传输过来的，默认为HttpServer
     * @return string 返回json_encode后的指令
     */
    public static function setCmd($fd, $cmd, $deviceMac, $data = [], $from = 'httpserver')
    {
        $data['deviceid'] = $deviceMac;

        return json_encode([
            'type'=>'command',
            'wsfd' => $fd,
            'command' => $cmd,
            'from' => $from,
            'deviceid'=> $deviceMac,
            'time' =>date('Y-m-d H:i:s'),
            'data' => $data
        ]);
    }
}